<!DOCTYPE html>
<html>
<!--author:zk-->
<head>
  <title>修改密码</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="keywords" content="New Theme Forms template Responsive, Login form web template,Flat Pricing tables,Flat Drop downs  Sign up Web Templates, Flat Web Templates, Login sign up Responsive web template, SmartPhone Compatible web template, free WebDesigns for Nokia, Samsung, LG, SonyEricsson, Motorola web design" />
  <script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); } </script>
  <!-- Custom Theme files -->
  <link href="css/style.css" rel="stylesheet" type="text/css" media="all" />
  <!-- //Custom Theme files -->
  <!-- web font -->
  <link href="//fonts.googleapis.com/css?family=Raleway:100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet">
  <!-- //web font -->
  <style>
    .button{
      font-size: 1em;
      color: #fff;
      background: transparent;
      border: 1px solid #fff;
      outline: none;
      cursor: pointer;
      padding: .6em 1em;
      -webkit-appearance: none;
      width: 100%;
      margin-top: 2em;
      transition: 0.5s all;
      -webkit-transition: 0.5s all;
      -moz-transition: 0.5s all;
      -o-transition: 0.5s all;
      -ms-transition: 0.5s all;
    }
    .button:hover {
      background: #FF9800;
      letter-spacing: 3px;
      color: #fff !important;
      border-color: #ff9800 !important;
    }
  </style>
</head>
<body>
<!-- main -->
<div class="main">
  <h1>修改密码</h1>
  <div class="main-w3lsrow" style="display: flex; justify-content: center">
    <!-- login form -->
    <!-- sign up form -->
    <div class="login-form login-form-left" >
      <div class="agile-row" >
        <div class="login-agileits-top" >
          <form>
            <p>旧密码</p>
            <!--							required表示必填项-->
            <input type="password" id="password" class="password" name="password" required="">
            <p>新密码</p>
            <input type="password" id="newPassword" class="password" name="newPassword" required="">
            <p>确认密码</p>
            <input type="password" id="confirmPassword" class="password" name="confirmPassword" required="">
            <input type="button" class="button" value="确认修改" onclick="updatePassword()">
          </form>
        </div>
      </div>
    </div>
    <div class="clear"> </div>
  </div>
</div>
<script type='text/javascript' src='js/jquery.min.js'></script>
<script type='text/javascript' src='js/plugins.js'></script>
<script type='text/javascript' src='js/script.js'></script>
<script type='text/javascript' src='js/particles.js'></script>
<script type='text/javascript' src='js/aos.js'></script>
<script type='text/javascript' src='js/prism.js'></script>
<script type='text/javascript' src='js/gravatar.js'></script>
<script type="text/javascript">
  var userId=localStorage.getItem("userId");
  getUser();
  function updatePassword() {
    //获取旧密码
    let oldPassword = localStorage.getItem("password");
    //获取cookie中的id
    let userId = localStorage.getItem("userId");
    //用户输入的密码
    const password = $("#password").val();
    //用户输入的新密码
    const newPassword = $("#newPassword").val();
    //加密后的新密码
    var md5_newPassword=hex_md5(newPassword);
    //确定密码
    const confirmPassword = $("#confirmPassword").val();
    if(isEmpty(oldPassword)||isEmpty(newPassword)||isEmpty(confirmPassword)){
      alert("请确保三个输入框都不为空！");
    }else{
      //旧密码等于输入的加密之后密码
      if (oldPassword===hex_md5(password)){
        //新密码等于确认密码输入的密码
        if (newPassword===confirmPassword){
          var user={
            "id" : userId,
            "password" :md5_newPassword,
          };
          $.ajax({
            //修改密码连接的url
            url:"http://192.168.47.129:8010/user/updatePassword",
            type:"post",
            contentType:"application/json;charset=utf-8",
            data:JSON.stringify(user),
            success: function (data) {
              //更新密码更新session 采用id和密码全部覆盖
              localStorage.setItem("userId",userId);
              localStorage.setItem("password",md5_newPassword);
              //跳转
              location.assign("index.html");
            },error: function() {
              alert("something went wrong");
            }
          })
        }else{
          alert("请确认二次输入密码正确！！！");
        }
      }else{
        alert("旧密码输入错误！！！");
      }
    }
  }
  //判断字符串是否为空的函数 true表示是空字符串
  function isEmpty(password) {
    if(password === '' || password.trim().length === 0){
      return true;
    }else{
      return false;
    }
  }
  function getUser(){
    if (userId==null){
      location.assign("login.html");
    }
  }
  function logout(){
    localStorage.clear();
  }

</script>
</body>
</html>